<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品添加</title>
<link rel="stylesheet" href="../css/good_add.css">
<script src="../js/jquery-1.12.4.min.js"></script>
<style type="text/css">
#message {
	resize: none;
	width: 76%;
	height: 180px;
}

#version {
	resize: none;
	width: 76%;
}
</style>
</head>
<body>
	<div id="forms" class="mt10">
		<div class="box">
			<div class="box_border">
				<div class="box_top" style="text-align: center;">
					<b class="pl15" style="font-size: 20px;">修改商品信息</b><br>
					<b class="pl15" style="font-size: 20px;color: red;">请在提交修改前上传图片!!!</b>
				</div>
				<div class="box_center">
					<div class="jqtransform" id="addNewsForm">
						<table class="form_table pt15 pb15" width="100%" border="0"
							cellpadding="0" cellspacing="0">
							<tr>
								<td class="td_right col-sm-3 col-md-6">商品类型：</td>
								<td><select class="select" onchange="changeSelect()" name="tid" id="tid" value="0">
										<option value="0">请选择类型</option>
										<c:forEach var="type" items="${types }" varStatus="status">
											<option id="option" class="type" value="${type.tid }">${type.tname }</option>
										</c:forEach>
								</select></td>
								<td class="td_right col-sm-3 col-md-6">商品名称：</td>
								<td><input type="text" id="gname" name="gname"
									class="input-text lh30 col-sm-3 col-md-6" size="40"
									placeholder="请输入商品名称"></td>
							</tr>

							<tr>
								<td class="td_right">商品数量：</td>
								<td><input type="number" id="num" name="num"
									class="input-text" size="40" min="0" value="" placeholder="请输入商品数量">
								</td>
								<td class="td_right">商品价格：</td>
								<td><input type="number" id="price" name="price"
									class="input-text lh30" size="40" min="0" placeholder="请输入商品价格">
								</td>
							</tr>

							<tr>
								<td class="td_right">商品颜色：</td>
								<td><input type="text" id="mycolor" name="mycolor"
									class="input-text" size="40" value="" placeholder="请输入商品颜色">
								</td>
									<td class="td_right">商品版本：</td>
								<td><input type="text" id="version" name="version"
									class="input-text" size="40" value="" placeholder="请输入版本信息">
								</td>
							</tr>

							<tr>
								<td class="td_right">图片：</td>
								<td colspan="3">
									<form action="../doUpload.jsp" method="post" enctype="multipart/form-data" target="nm_iframe">
										<input type="file" id="pic" name="pic"
										class="input-text lh30" size="40" multiple="multiple">
										<input type="submit" value="上传图片"  />
									</form>
								</td>
								<iframe id="id_iframe"  name="nm_iframe" style="display:none;">
									<input id="picUrl" value="">
								</iframe>
							</tr>
							<tr>
								<td class="td_right">商品详情：</td>
								<td colspan="3">
									<div>
										<textarea class="input-text lh30" id="message"></textarea>
									</div>
								</td>
							</tr>
							<tr>
								<td colspan="4" align="center"><input style="width: 200px;" type="button"
									name="button" onclick="submits()"
									value="提交修改">
								</td>
							</tr>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		$(function(){
			var tid = sessionStorage.getItem("modifyGood_tid");
			var gname = sessionStorage.getItem("modifyGood_gname");
			var num = sessionStorage.getItem("modifyGood_num");
			var price = sessionStorage.getItem("modifyGood_price");
			var color = sessionStorage.getItem("modifyGood_color");
			var version = sessionStorage.getItem("modifyGood_version");
			var message = sessionStorage.getItem("modifyGood_message");
			var pic = sessionStorage.getItem("good_pic");
			$("#tid").val(tid);
			$("#gname").val(gname);
			$("#num").val(num);
			$("#price").val(price);
			$("#mycolor").val(color);
			$("#version").val(version);
			$("#message").val(message);
			$("#pic").val(pic);
		});
		
		function changeSelect(){
			//console.log(666);
			console.log($(".type").find("option:selected"));
		}
		
		function submits() {
			var tid = $("#tid").val();
			var gname = $("#gname").val();
			var num = $("#num").val();
			var price = $("#price").val();
			var mycolor = $("#mycolor").val();
			var version = $("#version").val();
			var message = $("#message").val();
			
			var pic = sessionStorage.getItem("good_pic");
			console.log(pic);
			var gid = sessionStorage.getItem("modifyGood_gid");
			var page = sessionStorage.getItem("modifyGood_page");
			if(gid == null || gid==""){
				alert("请选择要修改的商品");
				return;
			}
			
			if (tid == 0) {
				alert("请选择商品类型!");
				return;
			}
			if (gname == null || gname == "") {
				alert("商品名不可为空!");
				return;
			}
			if (num == null || num == "" || num < 0) {
				alert("商品数量不可为空或小于0!");
				$("#num").focus();
				return;
			}
			if (price == null || price == "" || price < 0) {
				alert("商品价格不可为空或小于0!");
				return;
			}
			if (mycolor == null || mycolor == "") {
				alert("商品颜色不可为空!");
				return;
			}
			if (version == null || version == "") {
				alert("版本信息不可为空!");
				return;
			}
			if (message == null || message == "") {
				alert("商品详情不可为空!");
				return;
			}
			$.post("../../admin_good", {
				op : 'good_modify',
				gid : gid,
				tid : tid,
				gname : gname,
				pic : pic,
				inventory : num,
				price : price,
				color : mycolor,
				version : version,
				message : message
			}, function(data) {
				console.log(data);
				if (data > 0) {
					alert("修改成功!");
					$.post("../../admin_good",{
						op:'getGoodsbyPage',
						page:page
					},function(data){
						location.href = "http://localhost:8080/Xiaomi/admin/back/goods.jsp#type=0&page="+page;
					},"json");
				} else {
					alert("修改失败!");
				}
			});
		}
	</script>
</body>
</html>